// pages/add/shengcheng.js
Page({

     /**
      * 页面的初始数据
      */
     data: {
          width: 0,
          height: 0
     },

     /**
      * 生命周期函数--监听页面加载
      */
     onLoad: function(options) {
          var ctx = wx.createCanvasContext('canvas')
          let that = this;
          that.setData({
               width: wx.getSystemInfoSync().screenWidth * 0.92,
               height: wx.getSystemInfoSync().screenWidth * 0.92 * 1.42
          });
          var width = that.data.width;
          var height = that.data.height;
          ctx.save()
          ctx.setFillStyle('white')
          ctx.save(); // 先保存状态 已便于画完圆再用
          //先画底图
          this.roundRect2(ctx, 0, 0, width, height, 10, true, '#000')
          ctx.fillRect(0, 0, width, height)
          //画完底图画图集首图
          ctx.drawImage('../images/1.jpeg', 0, 0, width, height * 0.704)
          //文字1
          ctx.setFillStyle('#303030')
          ctx.setFontSize(14)
          ctx.fillText('难道还要我教你怎么泡我吗？', width * 0.04 + 10, height * 0.74 + 20)
          ctx.fillText('还不赶快扫码～', width * 0.04 + 10, height * 0.74 + 38)

          //文字2
          ctx.setFillStyle('#BABABA')
          ctx.setFontSize(12)
          ctx.fillText('长按识或微信扫一扫小程序码，', width * 0.04 + 10, height - 38)
          ctx.fillText('看看Ta的私房照', width * 0.04 + 10, height - 20)

          //小程序码

          ctx.drawImage('../images/ma.png', width - 15 - width * 0.3, height * 0.74 + 10, width * 0.3, width * 0.3)
   
          ctx.draw()
     },
     roundRect2: function(ctx, x, y, w, h, r, stroke, color) {
          ctx.save();
          ctx.beginPath()
          if (stroke) {
               ctx.setStrokeStyle(color);
          } else {
               ctx.setFillStyle(color);
          }
          x += 0.5;
          y += 0.5;
          ctx.setGlobalAlpha(0)
          ctx.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5);

          ctx.moveTo(x + r, y);

          ctx.lineTo(x + w - r, y);

          ctx.arc(x + w - r, y + r, r, Math.PI * 1.5, Math.PI * 2);

          ctx.lineTo(x + w, y + h - r);

          ctx.arc(x + w - r, y + h - r, r, 0, Math.PI * 0.5);

          ctx.lineTo(x + r, y + h);

          ctx.arc(x + r, y + h - r, r, Math.PI * 0.5, Math.PI);

          ctx.lineTo(x, y + r);

          if (stroke) {
               ctx.stroke();
          } else {
               ctx.fill();
          };
          ctx.clip();
          ctx.setGlobalAlpha(1)

     },
	saveImg: function () {
		let that = this;
		//保存临时图片
		wx.canvasToTempFilePath({
			x: 0,
			y: 0,
			canvasId: 'canvas',
			quality: 1,
			success: function (res) {
				console.log("get tempfilepath(success) is:", res.tempFilePath)
				//将图片保存在系统相册中(应先获取权限，)
				wx.saveImageToPhotosAlbum({
					filePath: res.tempFilePath,
					success(res) {
						wx.showToast({
							icon: 'success',
							title: '图片保存成功',
						})
						console.log("save photo is success")
					},
					fail: function () {
						console.log("save photo is fail")
					}
				})
			},
			fail: function (e) {
				console.log(e)
			}
		})
	}
})